//
//  XSComposeViewController.swift
//  WeiDo
//
//  Created by XISHAN on 16/3/11.
//  Copyright © 2016年 XISHAN. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD


class XSComposeViewController: UIViewController {

    //toolBar是否跟随键盘的动画
    lazy var isToolBarAnimation:Bool = true
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
       // MARK: - 私有方法
    // 切换键盘的方法
    private func switchKeyboard(){
        




        

        
    }

    
    
    
    
    // 选择图片的方法
    private func selectedPicture(){
        
        let vc = UIImagePickerController()
        vc.delegate = self
        
        self.presentViewController(vc, animated: true, completion: nil)
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    

    
    // 设置导航栏内容
    private func setupNavigation(){
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", target: self, action: "back")
        navigationItem.titleView = titleView
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightButton)
        navigationItem.rightBarButtonItem?.enabled = false
    }
    
    @objc private func back(){
        self.dismissViewControllerAnimated(true, completion: nil)
        
    }
    
    // MARK: - 懒加载textView控件
    private lazy var textView: XSTextView = {
        let textView = XSTextView(frame: CGRectZero, textContainer: nil)
        textView.placeholder = "君不见黄河之水天上来,奔流到海不复还.君不见高堂明镜悲白发,朝如青丝暮成雪.人生得意须纵欢,莫使金樽空对月.~~"
        textView.font = UIFont.systemFontOfSize(15)
        textView.delegate = self
        return textView
    }()
    
    // 右边的发送按钮
    private lazy var rightButton: UIButton = {
        let button = UIButton()
        // 设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: "common_button_orange_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_orange"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Disabled)
        // 设置不同状态下文字颜色
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Disabled)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        // 设置文字
        button.setTitle("发送", forState: UIControlState.Normal)
        // 设置字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        // 设置按钮的大小
        button.frame.size = CGSize(width: 40, height: 34)
        // 设置不可用
        //        button.enabled = false
        return button
    }()
    
    
    //懒加载titleView
    private lazy var titleView: UILabel = {
        let label = UILabel()
        // 指定换行
        label.numberOfLines = 0
        // 文字居中
        label.textAlignment = .Center
        if let screenName = XSUserAccountViewModel.sharedAccountViewModel.userAccount?.screen_name {
            let text = "发微博\n\(screenName)"
            // 初始化一个带有属性的文字
            let attrText = NSMutableAttributedString(string: text)
            //  attrText.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 3))
            // 定义需要设置的属性
            let attrDict = [
                NSForegroundColorAttributeName: UIColor.lightGrayColor(),
                NSFontAttributeName: UIFont.systemFontOfSize(14),
                NSStrokeWidthAttributeName: 2,
                NSStrokeColorAttributeName: UIColor.redColor()
            ]
            // 求出昵称所在的范围
            let range = (text as NSString).rangeOfString(screenName)
            // 添加属性
            attrText.addAttributes(attrDict, range: range)
            // 设置属性文字
            label.attributedText = attrText
        }else {
            label.text = "发微博"
        }
        label.sizeToFit()
        
        return label
    }()

    //懒加载ToolBar
    private lazy var composeToolBar: XSComposeToolBar = {
        let toolBar = XSComposeToolBar(frame: CGRectZero)
        // 设置里面按钮点击要执行的逻辑
        toolBar.composeToolBarBtnDidClick = {[weak self] (type: XSComposeToolBarButtonType) in
            switch type {
            case .Picture:
                // 选择图片
                self?.selectedPicture()
            case .Mention:
                print("@")
            case .Trend:
                print("#")
            case .Emoticon:
                self?.switchKeyboard()
            case .Add:
                print("加号")
            }
        }
        return toolBar
    }()
    
}

//发送按钮的状态
extension XSComposeViewController: UITextViewDelegate {
    
    func textViewDidChange(textView: UITextView) {
        // 设置右边按钮的可用状态
        navigationItem.rightBarButtonItem?.enabled = textView.hasText()
    }
}

//图片选择的控制器代理方法
extension XSComposeViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage ,info: [String : AnyObject]?){
        let scaleImage = image.scaleToWidth(600)
        picker.dismissViewControllerAnimated(true, completion: nil)
        
        
    }
 
    func imagePickerControllerDidCancel(picker: UIImagePickerController){
        print("用户取消")
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
}


// MARK: - 初始化空间方法
extension XSComposeViewController{
    private func setupUI(){
        view.backgroundColor = UIColor.whiteColor()
        setupNavigation()
        
        // 1. 添加控件
        view.addSubview(textView)
        view.addSubview(composeToolBar)
        
        // 2. 添加约束
        textView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        composeToolBar.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.bottom.equalTo(self.view)
            make.height.equalTo(40)
            
        }
        
        
    }

}
